<?php
/**
 * Created by PhpStorm.
 * User: Zhangxy
 * Date: 18/08/16
 * Time: 17:55
 */


/**
 * 打印不退出
 */
if (!function_exists('mm')) {
    /**
     * Dump the passed variables and end the script.
     *
     * @param  mixed
     * @return void
     */
    function mm()
    {
        array_map(function ($x) {
            (new \Illuminate\Support\Debug\Dumper)->dump($x);
        }, func_get_args());
    }
}


/**
 * 判断一个对象是否为空
 */
if (!function_exists('is_empty')) {
    function is_empty($obj)
    {
        if (empty($obj)) {
            return true;
        }
        if ($obj instanceof \Illuminate\Database\Eloquent\Collection) {
            return $obj->isEmpty();
        } elseif ($obj instanceof \Illuminate\Database\Eloquent\Model) {
            return empty($obj->{$obj->getKeyName()});
        } elseif (is_object($obj)) {
            return $obj == new stdClass();
        }
        return false;
    }
}


/**
 * 递归显示当前指定目录下所有文件
 * 使用dir函数
 * @param string $dir 目录地址
 * @param boolean $recursion 是否递归
 * @param string $assign_filename 指定文件名称
 * @return array $files 文件列表
 */
function getFiles($dir, $recursion = false, $assign_filename = '')
{
    $files = array();

    if (!is_dir($dir)) {
        return $files;
    }

    $d = dir($dir);
    while (false !== ($file = $d->read())) {
        if ($file != '.' && $file != '..') {
            $filename = $dir . "/" . $file;
            if (is_file($filename)) {
                if (!empty($assign_filename)) {
                    if (false !== stripos($filename, $assign_filename)) {
                        $files[] = $filename;
                    }
                } else {
                    $files[] = $filename;
                }
            } else {
                if ($recursion) {
                    $files = array_merge($files, getFiles($filename, $recursion, $assign_filename));
                }
            }
        }
    }
    $d->close();
    return $files;
}

